/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include <向量.h>
#include <材质/材质.h>

#include "材质纹理JIT函数绑定.h"

#include <core/shader_noise.h>
#include <core/shader_texture.h>





void f_JIT_1D噪波生成(uint32 num, vec2* offset, vec2* scale, float32* 强度, bool 单例[3], S_F32Array* 噪波, float32 n) {
	if (!num) return;

	f_core_array_resize((S_Array*)噪波, num);

	float32* 噪波_ptr = 噪波->ptr_userData;

	for (uint32 i = 0; i < num; ++i) {
		vec2 c{};

		
		if (单例[0]) c += offset[0];
		else c += offset[i];
		c += n;

		if (单例[1]) c *= scale[0];
		else c *= scale[i];


		float32 a;
		if (单例[2]) a = 强度[0];
		else a = 强度[i];

		噪波_ptr[i] = a * f_glsl_simplexNoise(c);
	}
}

void f_JIT_2D噪波生成(uint32 num, vec2* offset, vec2* scale, float32* 强度, bool 单例[3], S_Vec2Array* 噪波) {
	if (!num) return;

	f_core_array_resize((S_Array*)噪波, num);

	vec2* 噪波_ptr = 噪波->ptr_userData;

	for (uint32 i = 0; i < num; ++i) {
		vec2 c;
		if (单例[0]) c = offset[0];
		else c = offset[i];

		if (单例[1]) c += scale[0];
		else c += scale[i];

		float32 a;
		if (单例[2]) a = 强度[0];
		else a = 强度[i];

		vec3 noise = f_perlin2DNoise(c, true);
		噪波_ptr[i].x = noise.x * a;
		噪波_ptr[i].y = noise.y * a;
	}
}

void f_JIT_3D噪波生成(uint32 num, vec3* offset, vec3* scale, float32* 强度, bool 单例[3], S_Vec3Array* 噪波) {
	if (!num) return;

	f_core_array_resize((S_Array*)噪波, num);

	vec3* 噪波_ptr = 噪波->ptr_userData;

	for (uint32 i = 0; i < num; ++i) {
		vec3 c;
		if (单例[0]) c = offset[0];
		else c = offset[i];

		if (单例[1]) c += scale[0];
		else c += scale[i];

		float32 a;
		if (单例[2]) a = 强度[0];
		else a = 强度[i];

		vec3 noise = f_perlin2DNoise({c.x, c.y}, true);
		噪波_ptr[i] = noise * a;
	}
}

S_材质* export_create_material(uint32 设备ID) {
	S_材质* 材质 = f_material_createPBR();

	return 材质;
}

int32 f_JIT_getGPU纹理ID(S_纹理* 纹理) {
	return 纹理->m_TexID;
}


void f_node_所有材质纹理节点JIT初始化() {
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_glsl_simplexNoise), "float32", "vec2 uv", f_glsl_simplexNoise);
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_perlin2DNoise), "vec3", "vec2 uv, bool revert", f_perlin2DNoise);
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_texture_Gradient), "vec3", "vec3 uv, int type", f_texture_Gradient);

	f_代码构建_添加JIT初始化函数指针(DEF_S(f_JIT_1D噪波生成), "void", "uint32 num, vec2* offset, vec2* scale, float32* 强度, bool 单例[3], S_F32Array* 噪波, float32 n", f_JIT_1D噪波生成);


	f_代码构建_添加JIT初始化函数指针(DEF_S(f_JIT_getGPU纹理ID), DEF_S(int32), "S_纹理*", f_JIT_getGPU纹理ID);
}


